#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
void reader(int pipefd)
{
    int cnt = 3;
    char buffer[1024];
    while(cnt--)
    {
        read(pipefd,buffer,1024);
        printf("%s\n",buffer);
        sleep(1);
    }
    printf("reader close pipefd\n");
    close(pipefd);
}
void writer(int pipefd)
{
    char buffer[1024] = "Hello,pipe";
    while(1)
    {
        write(pipefd,buffer,strlen(buffer));
        sleep(1);
    }
    close(pipefd);
}


int main()
{
    int pipefd[2];
    int n = pipe(pipefd);
    if(n < 0) return 1;

    pid_t id = fork();
    if(id == 0)
    {
        //child
        close(pipefd[0]);
        writer(pipefd[1]);
        exit(0);
    }
    //father
    close(pipefd[1]);
    reader(pipefd[0]);

    int status = 0;
    waitpid(id,&status,0);
    printf("exit signal:%d\n",status&0x7F);
    return 0;
}